package com.libui.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.Point
import android.os.Build
import android.view.Display
import android.view.WindowManager
import com.libbase.utils.DensityUtils

class WindowUtils {
    companion object {
        val instance: WindowUtils by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            WindowUtils()
        }
    }

    @SuppressLint("InternalInsetResource", "DiscouragedApi")
    fun getStatusBarHeight(context: Context): Int {
        return DensityUtils.instance.px2dp(context, getStatusBarPixelHeight(context).toFloat())
    }

    @SuppressLint("InternalInsetResource", "DiscouragedApi")
    fun getStatusBarPixelHeight(context: Context): Int {
        val resourceId: Int =
            context.applicationContext.resources.getIdentifier(
                "status_bar_height",
                "dimen",
                "android"
            )

        return if (resourceId != 0) {
            context.resources.getDimensionPixelSize(resourceId)
        } else {
            0
        }
    }

    /**
     * 获取真实屏幕高度
     *
     * @return
     */
    private fun getWindowHeight(activity: Activity): Int {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            activity.windowManager.currentWindowMetrics.bounds.height()
        } else {
            val point = Point()
            @Suppress("DEPRECATION")
            activity.windowManager.defaultDisplay?.getRealSize(point)
            point.y
        }
    }

    /**
     * ### Get real screen size
     * @param context Context
     * @return [Point]?
     */
    fun getRealScreenSize(context: Context): Point {

        val screenSize = Point()

        val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val defaultDisplay = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            context.display
        } else {
            @Suppress("DEPRECATION")
            windowManager.defaultDisplay
        }

        @Suppress("DEPRECATION")
        defaultDisplay?.getRealSize(screenSize)

        return screenSize
    }

    /**
     * 获取虚拟导航栏(NavigationBar)是否显示, Activity onAttachedToWindow 后调用
     * @return true 表示虚拟导航栏显示，false 表示虚拟导航栏未显示
     */
    fun hasNavigationBar(activity: Activity): Boolean {
        val point = Point()
        @Suppress("DEPRECATION")
        activity.windowManager.defaultDisplay?.getSize(point)

        val safeInsetTop: Int
        val safeInsetBottom: Int
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            safeInsetTop =
                activity.window.decorView.rootWindowInsets?.displayCutout?.safeInsetTop ?: 0
            safeInsetBottom =
                activity.window.decorView.rootWindowInsets?.displayCutout?.safeInsetBottom ?: 0
        } else {
            safeInsetTop = 0
            safeInsetBottom = 0
        }

        // 沉浸式状态栏，activityHeight包括状态栏高度
        val activityHeight: Int = point.y
        val windowHeight: Int = getWindowHeight(activity) - safeInsetTop - safeInsetBottom

        return activityHeight != windowHeight
    }

    /**
     * 获取导航栏高度
     * @return Int 导航栏高度
     */
    @SuppressLint("InternalInsetResource", "DiscouragedApi")
    fun getNavigationBarHeight(activity: Activity): Int {
        if (!hasNavigationBar(activity)) {
            return 0
        }

        val resourceId: Int =
            activity.resources.getIdentifier("navigation_bar_height", "dimen", "android")
        return if (resourceId != 0) {
            activity.resources.getDimensionPixelSize(resourceId)
        } else {
            0
        }
    }
}